﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GPMS.Web.Controllers
{
    using GPMS.BLL;
    using GPMS.Model;
    public class VerifyController : BaseController
    {

        /// <summary>
        /// 检测是否有重复专业
        /// </summary>
        /// <param name="SpecialtyName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public JsonResult Specialty(string SpecialtyName, string key)
        {
            if (SpecialtyName.Equals(key, StringComparison.CurrentCultureIgnoreCase))
            {
                return Json(true);
            }
            return Json(!SpecialtyHelper.IsExist(SpecialtyName, gpmsDb));
        }

        /// <summary>
        /// 检测是否有重复角色
        /// </summary>
        /// <param name="RoleName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public JsonResult Role(string RoleName, string key)
        {
            if (RoleName.Equals(key, StringComparison.CurrentCultureIgnoreCase))
            {
                return Json(true);
            }
            return Json(!RoleHelper.IsExist(RoleName, gpmsDb));
        }

        /// <summary>
        /// 检查用户名是否重复
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public JsonResult CheckUserIsExist(string UserName,string key)
        {
            if (string.IsNullOrEmpty(UserName))
            {
                UserName = Request["User.UserName"];
            }
            if (UserName.Equals(key,StringComparison.CurrentCultureIgnoreCase))
            {
                return Json(true);
            }
            return Json(!gpmsDb.UserInfos.Any(u => u.UserName == UserName && u.IsDeleted==false));
        }

        /// <summary>
        /// 检测学号/工号是否存在
        /// </summary>
        /// <param name="UserIdentity"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public JsonResult UserIdentity(string UserIdentity, string key)
        {
            if (UserIdentity.Equals(key, StringComparison.CurrentCultureIgnoreCase))
            {
                return Json(true);
            }
            return Json(!gpmsDb.UserInfos.Any(u => u.UserIdentity == UserIdentity && u.IsDeleted==false));
        }


        public JsonResult ReceiveName(string Name, string key)
        {
            if (string.IsNullOrEmpty(Name))
            {
                Name=Request["ReceiverName"];
            }
            if (string.IsNullOrEmpty(Name))
            {
                Name=Request["SenderName"];
            }
            if (Name.Equals(key,StringComparison.CurrentCultureIgnoreCase))
            {
                return Json(true);
            }
            return Json(gpmsDb.UserInfos.Any(u => u.UserName == Name && u.IsDeleted == false));
        }

        public JsonResult AnswerGroup(string Name, string key)
        {
            if (Name.Equals(key))
            {
                return Json(true);
            }
            return Json(!AnswerGroupHelper.IsExist(Name, gpmsDb));
        }

        public JsonResult AnswerGroupMember(string UserID, string key)
        { 
            Guid id=AccountHelper.GetLonginUserSpecialty(Request,gpmsDb);
            Setting setting = SettingHelper.Get(gpmsDb);
            string satus=GPMS.Extension.UserStatusEnum.Answering.ToString();
            return Json(gpmsDb.UserInfos
                .Any(u => u.SpecialtyID == id && 
                    ((u.UserName == UserID && u.Roles.Any(r=>r.RoleID==setting.TeacherRole)) ||
                    (u.UserName == UserID && u.Roles.Any(r=>r.RoleID==setting.StudentRole)&& u.Status==satus)
                    )
                ));
        }
    }
}
